package com.vpx.server.controller.system;

import cn.hutool.core.collection.CollUtil;
import com.vpx.server.framework.base.ResultMsg;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @Description
 * @Author PC
 * @Date 2023/11/19 16:01
 **/
@RestController
@RequestMapping(value = "/file")
public class FileController {

    @Value("${file.path}")
    public String filePath;

    @RequestMapping("/upload")
    public ResultMsg upload(@RequestPart MultipartFile file) {
        List<String> list = null;
        try {
            list = saveFile(new MultipartFile[]{file});
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return ResultMsg.success(list);
    }

    @RequestMapping("/uploadList")
    public ResultMsg uploadList(@RequestPart MultipartFile[] files) {
        List<String> list = null;
        try {
            list = saveFile(files);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return ResultMsg.success(list);
    }

    @GetMapping(value = "/download")
    public void download(HttpServletResponse response, @RequestParam(value = "fileName") String fileName) throws IOException {
        String path = filePath+"\\"+fileName;
        File file = new File(path);
        if(file.exists()){ //判断文件父目录是否存在
            response.setContentType("application/force-download");
            response.setHeader("Content-Disposition", "attachment;fileName=" + fileName);

            byte[] buffer = new byte[1024];
            FileInputStream fis = null; //文件输入流
            BufferedInputStream bis = null;

            OutputStream os = null; //输出流
            try {
                os = response.getOutputStream();
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                int i = bis.read(buffer);
                while(i != -1){
                    os.write(buffer);
                    i = bis.read(buffer);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("----------file download" + fileName);
            try {
                bis.close();
                fis.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public List<String> saveFile(MultipartFile[] files) throws IOException {
        List<String> paths = new ArrayList<>();
        if (files.length <= 0) {
            return paths;
        }
        for (MultipartFile file : files) {
            String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            // 1：指定文件上传的目录
            String name = UUID.randomUUID()+ type;
            String path = filePath + "\\" + name ;
            File targetFile = new File(path);
            // 4：文件上传到指定的目录
            file.transferTo(targetFile);
            paths.add(name);
        }

        System.out.println("上传完毕!");
        return paths;
    }
}
